#pragma once
#include <chrono>
#include <string>
namespace cho =  std::chrono;


//typedef duration<long long, nano> nanoseconds;
//typedef duration<long long, micro> microseconds;
//typedef duration<long long, milli> milliseconds;
//typedef duration<long long> seconds;
//typedef duration<int, ratio<60> > minutes;
//typedef duration<int, ratio<3600> > hours;

//天类型
typedef cho::duration<int, std::ratio<60 * 60 * 24, 1>> days_type;

void localtime_ex(struct tm*  const _Tm, time_t const* const _Time);

class CDateTime
{
public:
    CDateTime();
    CDateTime(time_t t);
    CDateTime(const std::string& strTime, const std::string& strFmt = "%Y-%m-%d %H:%M:%S");
    CDateTime( cho::system_clock::time_point point);
   

    std::string ToString(const std::string& strFmt = "%Y-%m-%d %H:%M:%S");
    time_t ToMSecs();

    bool IsLeapYear(int nYear);
    int GetMonthDays();


    template<typename T, typename P>
    CDateTime operator + (cho::duration<T,P> dur)
    {
        auto dt = m_now + dur;
        return CDateTime(dt);
    }

    cho::seconds operator - (CDateTime& dt)
    {
        time_t now = cho::system_clock::to_time_t(m_now);
        return cho::seconds(now - dt.ToMSecs());
    }

    template<typename T, typename P>
    CDateTime& operator += (cho::duration<T,P> dur)
    {
        m_now +=  dur;
        time_t t = cho::system_clock::to_time_t(m_now);
        localtime_ex(&m_stTime, &t);
        return *this;
    }

    cho::system_clock::time_point Get()
    {
        return m_now;
    }
    

    int Year()
	{
		return m_stTime.tm_year + 1900;
	}
	int Month()
	{
		return m_stTime.tm_mon + 1;
	}
	int Day()
	{
		return m_stTime.tm_mday;
	}
	int Hour()
	{
		return m_stTime.tm_hour;
	}
    int Minute()
	{
		return m_stTime.tm_min;
	}
	int Second()
	{
		return m_stTime.tm_sec;
	}
	int DayOfYear()
	{
		return m_stTime.tm_yday;
	}

    CDateTime AddDays(int nDay);
    CDateTime AddMonths(int nMon);
    CDateTime AddYears(int nYear);
private:
    cho::system_clock::time_point m_now;
    struct tm m_stTime;
};



